<?php

import('com.yamvc.view.Mediator');
import("com.simpleApplication.view.blog.BlogIndexState");
import("com.simpleApplication.view.blog.BlogPostState");
import("com.simpleApplication.view.blog.BlogEvent");
import("com.simpleApplication.events.PostEvent");


class BlogMediator extends Mediator {
	
	/**
	 * Enter description here...
	 *
	 * @var BlogDataProxy
	 */
	private $_proxy;
	private $_posts;
	private $_addCommentError;
	public function __construct(ITemplate $template){
		parent::__construct($template);
		$this->_proxy = Model::instance()->retreaveProxy(BlogDataProxy::NAME);
		$this->addEventListener(BlogEvent::GET_NEWEST_EVENT,new EventListener($this,"showNewestHandler"));
		$this->addEventListener(BlogEvent::GET_POST,new EventListener($this,"showPost"));
		$this->addEventListener(PostEvent::SHOW_POST,new EventListener($this,"setShowPostState"));
		$this->addEventListener(PostEvent::ADD_COMMENT,new EventListener($this,"addCommentEventHandler"));
		$this->addEventListener(ApplicationEvent::DEFAULT_EVENT,new EventListener($this,"defaultEventHandler"));
		$this->addEventListener(ApplicationEvent::EVENT_DISPATCHED,new EventListener($this,"eventDispatched"));
		//Session::instance()->_blog_state=null;
	}
	
	public function setShowPostState(PostEvent $e){
		$state = new BlogPostState($e->getPostId(),$e->getPage());
		$this->changeState($state);
		Session::instance()->_blog_state=$state;
	}
	
	public function addCommentEventHandler(PostEvent $e){
		$user = Application::getAppInstance()->getUser();
		$comment="";
		if ($user->isAuthOk()==true){
			try{
				$comment = Request::instance()->post->commentData;
				if (strlen(trim($comment))==0){
					$this->_addCommentError="Could not be ampty";
				}else{
					$this->_proxy->addComment($user->getId(),$e->getPostId(),$comment);
				}
			}catch (NullPointerException $e){
				$this->_addCommentError="Hack?";
			}
		}else{
			$this->_addCommentError="Not authored user!! Hack?";
		}
		$state = new BlogPostState($e->getPostId());
		$this->changeState($state);
		Session::instance()->_blog_state=$state;
	}
	
	public function defaultEventHandler(ApplicationEvent $event){
		$state = new BlogIndexState();
		$this->changeState($state);
		Session::instance()->_blog_state=$state;		
	}
	public function eventDispatched(ApplicationEvent $e){
		try{
			$state=Session::instance()->_blog_state;
			$this->changeState($state);
		}catch (NullPointerException $e){
			$state = new BlogIndexState();
			$this->changeState($state);
			Session::instance()->_blog_state=$state;		
		}
	}
	
	public function showNewestHandler(BlogEvent $event){
		$this->_posts = array();
		$posts = $this->_proxy->getNewestPosts();
		$tpl = $this->createTemplate($this->getParam("postListTemplate"));
		$tpl->putVariable("posts",$posts);
		$this->_template->putVariable("content",$tpl);
	}
	
	public function showPost(BlogEvent $event){
		$user = Application::getAppInstance()->getUser();
		$postId = $event->getPostId();
		$post = $this->_proxy->getPost($postId);
		$tpl = $this->createTemplate($this->getParam("postTemplate"));
		$tpl->putVariable("auth",$user->isAuthOk());
		$tpl->putVariable("error",$this->_addCommentError);
		$tpl->putVariable("title",$post->title);		
		$tpl->putVariable("content",$post->content);
		$tpl->putVariable("postId",$post->id);
		$currentAction = Application::getAppInstance()->getCurrentAction();
		$tpl->putVariable("numPages",$this->_proxy->getNumberOfPages($post->id));
		$tpl->putVariable("url","http://localhost/yamvc/".$currentAction."/PostEvent/showPostEvent/".$post->id."/");
		$tpl->putVariable("addCommentUrl",'?action=blog&event=PostEvent:addCommentEvent('.$post->id.')');
		$this->_template->putVariable("content",$tpl);
		$tpl->putVariable("comments",$this->_proxy->getComments($postId,$event->getPage()-1));
	}
	
}

?>